﻿// ***********************************************************************
// Assembly         : YERIL_PI_INT
// Author           : Diego Bonilla
// Created          : 03-02-2014
//
// Last Modified By : Diego Bonilla
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="OrdenamientoEstructural.aspx.cs" company="Development Solutions CR">
//     Copyright (c) Development Solutions CR. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// The Login namespace.
/// </summary>
namespace YERIL_PI_INT.PaginasWebs.Modulos.Login
{
    /// <summary>
    /// Class OrdenamientoEstructural.
    /// </summary>
    public partial class OrdenamientoEstructural : System.Web.UI.Page
    {
        /// <summary>
        /// The mensaje error
        /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// The seleccion
        /// </summary>
        public string seleccion = "";
        /// <summary>
        /// The codigo
        /// </summary>
        static int codigo = 0;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Cargar_Lista();
                CargarGrid();

            }

            //lbxEstructura.Enabled = true;
            //lbxEstructura.Attributes.Add("enable", "enable");

        }

        /// <summary>
        /// Cargars the grid.
        /// </summary>
        public void CargarGrid()
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("Código Ordenamiento", Type.GetType("System.String"));
            dt.Columns.Add("Estructura", Type.GetType("System.String"));
            dt.Columns.Add("Sub-Estructura", Type.GetType("System.String"));
            dt.Columns.Add("Sub-Tipo Estructura", Type.GetType("System.String"));
           
            int cont = dt.Columns.Count;
            for (int i = 0; i < ListaOrdenamiento().Count; i++)
            {
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count - 1]["Código Ordenamiento"] = ListaOrdenamiento()[i].ORDEST_codestructura;
                dt.Rows[dt.Rows.Count - 1]["Estructura"] = ListaOrdenamiento()[i].ORDEST_estructura;
                dt.Rows[dt.Rows.Count - 1]["Sub-Estructura"] = ListaOrdenamiento()[i].ORDEST_subestructura;
                dt.Rows[dt.Rows.Count - 1]["Sub-Tipo Estructura"] = ListaOrdenamiento()[i].ORDEST_subtipoestructura;
                
            }

            gvOrdenamientoEstructural.DataSource = dt;
            gvOrdenamientoEstructural.DataBind();


        }
        /// <summary>
        /// Listas the ordenamiento.
        /// </summary>
        /// <returns>List{WSGestion.MOD_OrdenamientoEstructural}.</returns>
        private List<WSGestion.MOD_OrdenamientoEstructural> ListaOrdenamiento()
        {

            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            List<WSGestion.MOD_OrdenamientoEstructural> ListaOrdenamiento = wsgest.SEL_Grid_OrdenamientoEstructural().ToList();
            return ListaOrdenamiento;

        }

        /// <summary>
        /// Cargar_s the lista.
        /// </summary>
        private void Cargar_Lista()
        {
            try
            {
                WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                List<WSGestion.MOD_Estructura> ListaEstructura = wsgest.SEL_Grid_Estructura().ToList();
                this.lbxEstructura.DataSource = ListaEstructura;
                this.lbxEstructura.DataValueField = "EST_codestructura";
                this.lbxEstructura.DataTextField = "EST_nombre";
                this.lbxEstructura.DataBind();

                List<WSGestion.MOD_SubEstructuras> ListaSubEstructura = wsgest.SEL_Grid_SubEstructura().ToList();
                this.lbxSubEstructura.DataSource = ListaSubEstructura;
                this.lbxSubEstructura.DataValueField = "SUBEST_codsubest";
                this.lbxSubEstructura.DataTextField = "SUBEST_nombre";
                this.lbxSubEstructura.DataBind();

                List<WSGestion.MOD_Subtipo_Estructura> ListaSubtipoEstructura = wsgest.SEL_Grid_Subtipo_Estructura().ToList();
                this.lbxSubTipoEstructura.DataSource = ListaSubtipoEstructura;
                this.lbxSubTipoEstructura.DataValueField = "SUBTIPOEST_codsubtipoest";
                this.lbxSubTipoEstructura.DataTextField = "SUBTIPOEST_nombre";
                this.lbxSubTipoEstructura.DataBind();

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnAgregar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregar_Click(object sender, ImageClickEventArgs e)
        {
            Session["FormRetorno"] = "OrdenamientoEstructural.aspx";
            Session["FormAccion"] = "Agregar";
            Response.Redirect("Estructura.aspx");
        }

        /// <summary>
        /// Handles the Click event of the btnAgregarSubEstructura control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregarSubEstructura_Click(object sender, ImageClickEventArgs e)
        {
            Session["FormRetorno"] = "OrdenamientoEstructural.aspx";
            Session["FormAccion"] = "Agregar";
            Response.Redirect("SubEstructura.aspx");
        }

        /// <summary>
        /// Handles the Click event of the btnAgregarSubTipoEstructura control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregarSubTipoEstructura_Click(object sender, ImageClickEventArgs e)
        {
            Session["FormRetorno"] = "OrdenamientoEstructural.aspx";
            Session["FormAccion"] = "Agregar";
            Response.Redirect("SubTipoEstructura.aspx");
        }

        /// <summary>
        /// Handles the Click event of the btnGuardarOrdenamiento control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnGuardarOrdenamiento_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                int codEstructura = Convert.ToInt32(lbxEstructura.SelectedValue);
                int codSubEstructura = Convert.ToInt32(lbxSubEstructura.SelectedValue);
                int codSubTipoEstructura = Convert.ToInt32(lbxSubTipoEstructura.SelectedValue);
                int cant_ord = wsgest.SEL_Ordenamiento_EstructuralByIDs(codEstructura, codSubEstructura, codSubTipoEstructura);
                if (cant_ord == 0)
                {
                    if (wsgest.CRE_Ordenamiento_Estructural(codEstructura,codSubEstructura,codSubTipoEstructura))
                    {
                        MensajeError = "Estructura " + lbxEstructura.SelectedItem + "SubEstructura " + lbxSubEstructura.SelectedItem + "SubTipoEstructura " + lbxSubTipoEstructura.SelectedItem + " se ha agregado.";
                        CargarGrid();
                    }
                    else
                    {
                        MensajeError = "Procedimiento Almacenado no aceptado";
                    }

                }

                else
                {
                    MensajeError = "No es valido introducir el mismo ordenamiento estructural. Por favor, intente con otro de la lista.";
                }
                 
            }
            catch (Exception exc)
            {

                MensajeError = exc.Message.ToString();
            }
               
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the gvOrdenamientoEstructural control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void gvOrdenamientoEstructural_SelectedIndexChanged(object sender, EventArgs e)
        {
            seleccion = gvOrdenamientoEstructural.SelectedRow.Cells[2].Text + " , " + gvOrdenamientoEstructural.SelectedRow.Cells[3].Text + " , " + gvOrdenamientoEstructural.SelectedRow.Cells[4].Text;
            codigo = Convert.ToInt32(gvOrdenamientoEstructural.SelectedRow.Cells[1].Text);
            MensajeError = "Se ha seleccionado un ordenamiento estructural: " + seleccion;
        }

        /// <summary>
        /// Handles the Click event of the btnEliminar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnEliminar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (codigo == 0)
                {
                    MensajeError = "No se ha seleccionado un ordenamiento";
                }
                else
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    wsgest.DEL_Ordenamiento(codigo);
                    CargarGrid();
                }
            }
            catch (Exception exc)
            {

                MensajeError = exc.Message.ToString();
            }
        }
    }
}